package com.extension.company.view.details;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseFragment;
import com.extension.company.common.utils.ToastUtil;
import com.extension.company.contract.home.GoodsCommitFragmentContract;
import com.extension.company.model.entity.GoodsInfoDetail2;
import com.extension.company.presenter.home.GoodsCommentFragmentPresenter;
import com.extension.company.view.home.adapter.CommentAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * item页ViewPager里的评价Fragment
 */
public class GoodsCommentFragment extends BaseFragment<GoodsCommitFragmentContract.View, GoodsCommentFragmentPresenter>
		implements GoodsCommitFragmentContract.View {

	@BindView(R.id.tv_comment_count)
	TextView tv_comment_count;
	@BindView(R.id.tv_good_comment)
	TextView tv_good_comment;
	@BindView(R.id.comment_recycle)
	RecyclerView commentRecycle;
	@BindView(R.id.comment_radiogroup)
	RadioGroup commentRadiogroup;

	private Context context;
	private CommentAdapter commentAdapter;
	private List<GoodsInfoDetail2.DataBean.GoodsCommentBean> commentList;
	private List<GoodsInfoDetail2.DataBean.GoodsCommentBean> localList;
	private List<GoodsInfoDetail2.DataBean.GoodsCommentBean> noImageList;


	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		this.context = context;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (!EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().register(this);
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().unregister(this);
		}
	}

	@Override
	public GoodsCommitFragmentContract.View createView() {
		return this;
	}

	@Override
	public GoodsCommentFragmentPresenter createPresenter() {
		return new GoodsCommentFragmentPresenter();
	}

	@Override
	public void init() {
		commentList = new ArrayList<>();
		noImageList = new ArrayList<>();
		localList = new ArrayList<>();

		commentRecycle.setLayoutManager(new LinearLayoutManager(context));
		commentAdapter = new CommentAdapter(context, commentList);
		commentRecycle.setAdapter(commentAdapter);
	}

	@Override
	public int getMainLayout() {
		return R.layout.fragment_goods_comment;
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void commentData(List<GoodsInfoDetail2.DataBean.GoodsCommentBean> commentBeansList) {
		if (null != commentBeansList) {
			if (commentBeansList.size() == 0) {
				ToastUtil.showToast(context, "无任何评论");
			} else {
				localList = commentBeansList;
				commentList.addAll(commentBeansList);
				commentAdapter.notifyDataSetChanged();

				for (GoodsInfoDetail2.DataBean.GoodsCommentBean bean : commentBeansList) {
					if ("1".equals(bean.getIs_img())) {
						noImageList.add(bean);
					}
				}

			}
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO: inflate a fragment view
		View rootView = super.onCreateView(inflater, container, savedInstanceState);
		return rootView;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@OnCheckedChanged({R.id.all_comment, R.id.all_img})
	public void onRadioCheck(CompoundButton view, boolean ischanged) {
		switch (view.getId()) {
			case R.id.all_comment:
				if (ischanged) {
					commentList.clear();
					commentList.addAll(localList);
					commentAdapter.notifyDataSetChanged();
				}
				break;
			case R.id.all_img:
				if (ischanged) {
					commentList.clear();
					commentList.addAll(noImageList);
					commentAdapter.notifyDataSetChanged();
				}

				break;
			default:
				break;
		}

	}
}
